package com.example.demo;

import org.activiti.engine.*;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class ActivitiConfig {
    @Value("${spring.datasource.type}")
    private Class<? extends DataSource> dataSourceType;
    public static ApplicationContext applicationContext;
    @Bean(name = "myDataSource")
    @Qualifier("myDataSource")
    @ConfigurationProperties(prefix="spring.datasource.druid.master")
    public DataSource getMyDataSource(){
        return DataSourceBuilder.create().type(dataSourceType).build();
    }

    @Bean(name = "ProcessEngineConfiguration")
    public ProcessEngineConfiguration processEngineConfiguration(@Qualifier("myDataSource") DataSource dataSource)
    {
        ProcessEngineConfiguration config = new StandaloneProcessEngineConfiguration();
        config.setDataSource(dataSource);
        config.setLabelFontName("宋体");
        config.setActivityFontName("宋体");
        config.setAnnotationFontName("宋体");
        config.buildProcessEngine();
        return config;
    }

        @Bean(name = "ProcessEngine") // processEngineConfiguration
         public ProcessEngine getProsessEngien(@Qualifier("ProcessEngineConfiguration") ProcessEngineConfiguration pro) {
            pro.setLabelFontName("宋体");
            pro.setActivityFontName("宋体");
            pro.setAnnotationFontName("宋体");
             return pro.buildProcessEngine();
        }
        @Bean(name = "RuntimeService")
    public RuntimeService getRuntimeService(@Qualifier("ProcessEngine") ProcessEngine processEngine)
        {
            return processEngine.getRuntimeService();
        }
    @Bean(name = "RepositoryService")
    public RepositoryService getRepositoryService(@Qualifier("ProcessEngine") ProcessEngine processEngine)
    {
        return processEngine.getRepositoryService();
    }
    @Bean(name = "TaskService")
      public TaskService getTaskService(@Qualifier("ProcessEngine") ProcessEngine processEngine) {
            return processEngine.getTaskService();
      }

      @Bean(name = "FormService")
      public FormService getFormService(@Qualifier("ProcessEngine") ProcessEngine processEngine) {
            return processEngine.getFormService();
      }

      @Bean(name = "HistoryService")
      public HistoryService getHistoryService(@Qualifier("ProcessEngine") ProcessEngine processEngine) {
            return processEngine.getHistoryService();
      }

      @Bean(name = "ManagementService")
      public ManagementService getManagementService(@Qualifier("ProcessEngine") ProcessEngine processEngine) {
            return processEngine.getManagementService();
      }

      @Bean(name = "IdentityService")
      public IdentityService getIdentityService(@Qualifier("ProcessEngine") ProcessEngine processEngine) {
            return processEngine.getIdentityService();
      }


}
